package com.cnap.multiclouldmgmt.model.enums;

import cn.hutool.core.text.CharSequenceUtil;
import com.cnap.multiclouldmgmt.model.vm.VmCreateModel;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;

/**
 * 阶段二级分类
 *
 * @author xc
 **/
public enum StageDetailTypeEnum {

    //初始步骤
    ZERO("0-0", StageTypeEnum.INIT.getType(), 1, "初始步骤",0),
    // 申请云资源，完成开通
    ONE_ONE("1-1", StageTypeEnum.CLOUD_RESOURCE.getType(), 1, "申请云资源，完成开通",1),
    // 获取开通的云资源详情
    ONE_TWO("1-2", StageTypeEnum.CLOUD_RESOURCE.getType(), 2, "获取开通的云资源详情",2),
    // 确认需要开通的端口
    TOW_ONE("2-1", StageTypeEnum.SAFE_PORT.getType(), 1, "确认需要开通的端口",3),
    // 申请端口开通
    TOW_TOW("2-2", StageTypeEnum.SAFE_PORT.getType(), 2, "申请端口开通",4),
    // 等待端口开通完成
    TOW_THREE("2-3", StageTypeEnum.SAFE_PORT.getType(), 3, "等待端口开通完成",5),
    // 完成端口开通确认
    TOW_FOUR("2-4", StageTypeEnum.SAFE_PORT.getType(), 4, "完成端口开通确认",6),
    // 检查云资源状态
    THREE_ONE("3-1", StageTypeEnum.K8S.getType(), 1, "检查云资源状态",7),
    // 拉取安装所需资源
    THREE_TOW("3-2", StageTypeEnum.K8S.getType(), 2, "拉取安装所需资源",8),
    // 确认安装环境
    THREE_THREE("3-3", StageTypeEnum.K8S.getType(), 3, "确认安装环境",9),
    // 安装openssh
    THREE_FOUR("3-4", StageTypeEnum.K8S.getType(), 4, "安装openssh，ansible等软件",10),
    // 配置安装参数
    THREE_FIVE("3-5", StageTypeEnum.K8S.getType(), 5, "配置安装参数",11),
    // 完成K8s
    THREE_SIX("3-6", StageTypeEnum.K8S.getType(), 6, "完成K8s/Prometheus镜像库同步设置",12),
    // 完成K8s集群节点标签设置
    THREE_SEVEN("3-7", StageTypeEnum.K8S.getType(), 7, "完成K8s集群节点标签设置",13),
    // 完成应用镜像同步
    FOUR_ONE("4-1", StageTypeEnum.DISTRIBUTE.getType(), 1, "完成应用镜像同步",14),
    // 完成应用安装部署
    FOUR_TOW("4-2", StageTypeEnum.DISTRIBUTE.getType(), 2, "完成应用安装部署",15),
    //结束
    END("9-9", StageTypeEnum.END.getType(), 1, "完成步骤",99),


    // 注释
    UNKNOWN("--", "unknown", 0, "unknown",100),
    ;

    private final String detailType;
    private final String parentType;
    private final int order;
    private final String message;
    private final int stepOrder;

    StageDetailTypeEnum(String detailType, String parentType, int order, String message,int stepOrder) {
        this.detailType = detailType;
        this.parentType = parentType;
        this.order = order;
        this.message = message;
        this.stepOrder = stepOrder;
    }

    /**
     * get
     *
     * @return DetailType
     */
    public String getDetailType() {
        return detailType;
    }

    /**
     * get
     *
     * @return ParentType
     */
    public String getParentType() {
        return parentType;
    }

    /**
     * get
     *
     * @return Order
     */
    public int getOrder() {
        return order;
    }

    /**
     * get
     *
     * @return Message
     */
    public String getMessage() {
        return message;
    }

    /**
     * get
     *
     * @return StepOrder
     */
    public int getStepOrder() {
        return stepOrder;
    }

    /**
     * 获取当前枚举的前一个枚举值
     *
     * @return 结果
     */
    public StageDetailTypeEnum previous() {
        int ordinal = this.ordinal();
        StageDetailTypeEnum[] stageTypeEnums = StageDetailTypeEnum.values();
        for (StageDetailTypeEnum stageTypeEnum : stageTypeEnums) {
            if (stageTypeEnum.ordinal() == ordinal - 1) {
                return stageTypeEnum;
            }
        }
        return UNKNOWN;
    }

    /**
     * 获取当前枚举的后一个枚举值
     *
     * @return 结果
     */
    public StageDetailTypeEnum next() {
        int ordinal = this.ordinal();
        StageDetailTypeEnum[] stageTypeEnums = StageDetailTypeEnum.values();
        for ( StageDetailTypeEnum stageTypeEnum : stageTypeEnums) {
            if (stageTypeEnum.ordinal() == ordinal + 1) {
                return stageTypeEnum;
            }
        }
        return UNKNOWN;
    }

    /**
     * 根据类型获取枚举
     *
     * @param type 类型
     * @return 结果
     */
    public static StageDetailTypeEnum getStageDetailTypeEnumByType(String type) {
        if (CharSequenceUtil.isEmpty(type)) {
            return UNKNOWN;
        }
        StageDetailTypeEnum[] stageTypeEnums = StageDetailTypeEnum.values();
        for (StageDetailTypeEnum stageTypeEnum : stageTypeEnums) {
            if (stageTypeEnum.getDetailType().equals(type)) {
                return stageTypeEnum;
            }
        }
        return UNKNOWN;
    }

    public static int getMaxStepOrder(){
        return FOUR_ONE.stepOrder;
    }

    /**
     * 根据stepOrder 获取
     *
     * @param stepOrder 类型
     * @return 结果
     */
    public static StageDetailTypeEnum getStageDetailTypeEnumByType(Integer stepOrder) {

        for (StageDetailTypeEnum stageTypeEnum : StageDetailTypeEnum.values()) {
            if (stepOrder.equals(stageTypeEnum.getStepOrder())) {
                return stageTypeEnum;
            }
        }
        return UNKNOWN;
    }

    //获取重试的步骤
    public static StageDetailTypeEnum getReTryStep(VmCreateModel vmCreateModel){

        //如果发起建立虚机请求就挂了，那就
        if (StringUtils.equals(vmCreateModel.getType(),StageTypeEnum.CLOUD_RESOURCE.getType())){

            //发起请求虚机就挂了，那就重新开始
            if (vmCreateModel.getStepOrder() == ONE_ONE.stepOrder){
                return ZERO;
            }

            //如果是虚机建立部分挂了，那也重新开始
            if (vmCreateModel.getStepOrder() == ONE_TWO.stepOrder){
                return ZERO;
            }
        }

        //如果是2-4，说明创建的虚机ping不通
        if (StringUtils.equals(vmCreateModel.getType(),StageTypeEnum.SAFE_PORT.getType())){

            //还是要重试
            if (vmCreateModel.getStepOrder() == TOW_FOUR.stepOrder){
                return ZERO;
            }
        }

        //如果是k8s步骤
        if (StringUtils.equals(vmCreateModel.getType(),StageTypeEnum.K8S.getType())){

            //3-1 这一步，说明已经能ping通虚机了，要直接进行下面的步骤就好
            if (vmCreateModel.getStepOrder() == THREE_ONE.stepOrder){
                return THREE_ONE;
            }

            //3-2这一步开始拷贝包了，要重新拷贝，防止拷贝一半有问题
            if (vmCreateModel.getStepOrder() == THREE_TOW.stepOrder){
                return THREE_ONE;
            }

            //3-3解压包了，说明
            if (vmCreateModel.getStepOrder() == THREE_THREE.stepOrder){
                return THREE_THREE;
            }

            //3-4安装插件，说明插件装好了
            if (vmCreateModel.getStepOrder() == THREE_FOUR.stepOrder){
                return THREE_FOUR;
            }

            //3-5设置安装参数
            if (vmCreateModel.getStepOrder() == THREE_FIVE.stepOrder){
                return THREE_FIVE;
            }

            //3-6进行安装
            if (vmCreateModel.getStepOrder() == THREE_SIX.stepOrder){
                return THREE_SIX;
            }
        }

        return ZERO;
    }
}
